package com.shixun.eduservice.service.impl;

import com.shixun.eduservice.entity.EduCourse;
import com.shixun.eduservice.entity.EduCourseDescription;
import com.shixun.eduservice.entity.vo.CourseInfoVo;
import com.shixun.eduservice.entity.vo.CoursePublishVo;
import com.shixun.eduservice.mapper.EduCourseMapper;
import com.shixun.eduservice.service.EduChapterService;
import com.shixun.eduservice.service.EduCourseDescriptionService;
import com.shixun.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.eduservice.service.EduVideoService;
import com.shixun.servicebase.exceptionhandler.shixunException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-14
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService ChapterService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0) {

            throw new shixunException(20001,"添加课程信息失败");
        }
        String cid = eduCourse.getId();

        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);
        return cid;

    }


    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update =baseMapper.updateById(eduCourse);
        if(update==0){
            throw new shixunException(20001,"修改课程信息失败");
        }
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {

        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }
//删除课程

    //删小节
    @Override
    public void removeCourse(String courseId) {
        eduVideoService.removeVideoByCourseId(courseId);
    //删章节
        ChapterService.removeChapterByCourseId(courseId);
        //shanchu描述
        courseDescriptionService.removeById(courseId);
        //删除课程本身
        int result=baseMapper.deleteById(courseId);

        if(result==0){
            throw new shixunException(20001,"删除失败");
        }
    }


}
